#include "../inc/gui.bi"
#Include Once "fbgfx.bi"
#include once "ext/graphics/img_load.bi"

const as single Tile_Size = 48
const as integer xoffset = 16 'for aligning the window to the center
                              ' since we have 32 'extra' pixels
const as integer yoffset = 12 '24 pixels for y

extern Gui(8) as fb.image ptr

' Loads the images needed for drawWindow
Sub loadGui()
   
   gui(GUI_RIGHT) = ext.LoadImage("res/gfx/gui/right.png")
   gui(GUI_TOP_RIGHT) = ext.LoadImage("res/gfx/gui/top_right.png")
   gui(GUI_TOP) = ext.LoadImage("res/gfx/gui/top.png")
   gui(GUI_TOP_LEFT) = ext.LoadImage("res/gfx/gui/top_left.png")
   gui(GUI_LEFT) = ext.LoadImage("res/gfx/gui/left.png")
   gui(GUI_BOTTOM_LEFT) = ext.LoadImage("res/gfx/gui/bottom_left.png")
   gui(GUI_BOTTOM) = ext.LoadImage("res/gfx/gui/bottom.png")
   gui(GUI_BOTTOM_RIGHT) = ext.LoadImage("res/gfx/gui/bottom_right.png")
   gui(GUI_CENTER) = ext.LoadImage("res/gfx/gui/center.png")

End sub

' Unloads the images loaded for drawWindow
Sub UnloadGui()
   Dim x As Integer = GUI_RIGHT 

   While(x < GUI_CENTER)
      imagedestroy(gui(x))
      x+=1
   Wend  
   imagedestroy(gui(GUI_CENTER))

End Sub


' Draws windows in tile-sized blocks 
sub drawWindow(mapx as integer, mapy as integer,mapx2 as integer, mapy2 as integer,alpha_val as integer = 255)
        
    dim as integer x = (mapx * Tile_Size) + xoffset,y = (mapy * Tile_Size) + yoffset
    dim as integer x2 = (mapx2 * Tile_Size) + xoffset, y2 = (mapy2 * Tile_Size) + yoffset
    
    if(((mapy2 - mapy) > 0) and ((mapx2 - mapx) > 0)) then
        
        'top part
        putW x, y,gui(GUI_TOP_LEFT),alpha_val
                
        while((x + tile_size) < x2)
            x += Tile_Size
            putW x, y,gui(GUI_TOP),alpha_val
        wend
        putW x + Tile_Size, y,gui(GUI_TOP_RIGHT),alpha_val
                
        'Center part
        if ((mapy2 - mapy) > 2) then
            while((y + Tile_Size) < y2)
                y += Tile_Size
                x = (mapx * Tile_Size) + xoffset
                putW x, y,gui(GUI_LEFT),alpha_val
                while((x + tile_size) < x2)
                    x += Tile_Size
                    putW x, y,gui(GUI_CENTER),alpha_val
                wend
                putW x + Tile_Size, y,gui(GUI_RIGHT),alpha_val
            wend
        end if
        
        y += Tile_Size
        
        'Bottom part
        x = (mapx * Tile_Size) + xoffset
        putW x, y,gui(GUI_BOTTOM_LEFT),alpha_val
        
        while((x + Tile_Size) < x2)
            x += Tile_Size
            putW x, y,gui(GUI_BOTTOM),alpha_val
        wend
        putW x + Tile_Size, y,gui(GUI_BOTTOM_RIGHT),alpha_val
        
    end if            
    
    
    
end sub

sub putW(x as integer, y as integer,byref g as Fb.image ptr,alpha_val as integer)

    if alpha_val = 255 then
        put(x,y),g,trans
    else
        put(x,y),g,alpha,alpha_val
    end if     
    
end sub
